﻿using System.Linq;
using System.Web.Mvc;
using Localization;
using SinBiota.Model.Business.AuthenticationServices.Interfaces;
using SinBiota.Model.Business.PersistenceServices.Interfaces;
using SinBiota.Model.Entities;
using Sinbiota.Web.ActionFilters;

namespace Sinbiota.Web.Controllers
{
    public class ConservationUnitController : BaseController
    {
        private readonly IConservationUnitPersistenceServices _conservationUnitPersistenceServices;

        public ConservationUnitController(IConservationUnitPersistenceServices conservationUnitPersistenceServices, IUserPersistenceServices userPersistenceServices, IPermissionServices permissionServices)
        {
            _conservationUnitPersistenceServices = conservationUnitPersistenceServices;
        }

        //
        // GET: /ConservationUnit/
        [ImportMessagesFromTempData]
        [AdminPermission]
        public ActionResult Index()
        {
            return View(_conservationUnitPersistenceServices.FindAll());
        }

        [HttpPost]
        [AdminPermission]
        public ActionResult Index(FormCollection collection)
        {
            return View(_conservationUnitPersistenceServices.FindByNameLike(collection[SearchBoxName]));
        }

        //
        // GET: /ConservationUnit/Details/5
        [ImportMessagesFromTempData]
        [AdminPermission]
        public ActionResult Details(int id)
        {
            var conservationUnit = _conservationUnitPersistenceServices.FindById(id);
            return conservationUnit == null ? View("NotFound") : View(conservationUnit);
        }

        //
        // GET: /ConservationUnit/Create
        [ImportMessagesFromTempData]
        [AdminPermission]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /ConservationUnit/Create
        [HttpPost, ExportMessagesToTempData]
        [AdminPermission]
        public ActionResult Create(ConservationUnit conservationUnit)
        {
            var newConservationUnit = new ConservationUnit();
            TryUpdateModel(newConservationUnit);
            if (ModelState.IsValid)
            {
                _conservationUnitPersistenceServices.Save(newConservationUnit);
                SetInformationMessage(Messages.OperationCompletedSuccessfully);
                return RedirectToAction("Index");
            }
            return View(conservationUnit);
        }

        //
        // GET: /ConservationUnit/Edit/5
        [ImportMessagesFromTempData]
        [AdminPermission]
        public ActionResult Edit(int id)
        {
            var conservationUnit = _conservationUnitPersistenceServices.FindById(id);
            return conservationUnit == null ? View("NotFound") : View(conservationUnit);
        }

        //
        // POST: /ConservationUnit/Edit/5
        [HttpPost, ExportMessagesToTempData]
        [AdminPermission]
        public ActionResult Edit(int id, ConservationUnit conservationUnit)
        {
            var editConservationUnit = _conservationUnitPersistenceServices.FindById(id);
            TryUpdateModel(editConservationUnit);
            if (ModelState.IsValid)
            {
                _conservationUnitPersistenceServices.Update(editConservationUnit);
                SetInformationMessage(Messages.OperationCompletedSuccessfully);
                return RedirectToAction("Index");
            }
            return View(conservationUnit);
        }

        //
        // GET: /ConservationUnit/Delete/5
        [ImportMessagesFromTempData]
        [AdminPermission]
        public ActionResult Delete(int id)
        {
            var conservationUnit = _conservationUnitPersistenceServices.FindById(id);
            return conservationUnit == null ? View("NotFound") : View(conservationUnit);
        }

        //
        // POST: /ConservationUnit/Delete/5
        [HttpPost, ExportMessagesToTempData]
        [AdminPermission]
        public ActionResult Delete(int id, ConservationUnit conservationUnit)
        {
            var deleteConservationUnit = _conservationUnitPersistenceServices.FindById(id);
            if (deleteConservationUnit == null) return View("NotFound");
            _conservationUnitPersistenceServices.Delete(deleteConservationUnit);
            SetInformationMessage(Messages.OperationCompletedSuccessfully);
            return RedirectToAction("Index");
        }

        // AJAX: 
        //Get all ConservationUnits
        public JsonResult GetAll()
        {
            return Json(_conservationUnitPersistenceServices.FindAll().Select(x => new { x.Id, x.Name }).OrderBy(i => i.Name));
        }
    }

}
